package com.ly.mpayment.rest.system;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ly.mpayment.core.util.Config;
import com.ly.mpayment.core.util.JsonUtils;
import com.ly.mpayment.core.util.MapUtil;
import com.ly.mpayment.core.util.ResultMessage;
import com.ly.mpayment.core.util.StringUtils;
import com.ly.mpayment.persist.entity.ProvincialInformation;
import com.ly.mpayment.persist.service.provincialinformation.IProvincialInformationService;
import com.ly.mpayment.persist.service.user.IUserService;
import com.ly.mpayment.rest.AbsRest;

import net.sf.json.JSONObject;

@Component
@Path("system/province/")
public class ProvincialInformationRest extends AbsRest{

	@Autowired
	IUserService userService;

	@Autowired
	IProvincialInformationService proService;

	@POST
	@Path("searchProvince")
	public String searchProvince(String data ,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService, "provincename","provincecode","start","pageSize");
		if(result == null){
			JSONObject json = JsonUtils.converToJson(data);
			Map<String, Object> parameters = MapUtil.getMap();
			if(!StringUtils.isBlank(json.getString("provincename"))){
				parameters.put(":provincename", json.getString("provincename"));
			}
			if(!StringUtils.isBlank(json.getString("provincecode"))){
				parameters.put(":provincecode", json.getString("provincecode"));
			}
			return ResultMessage.getSucceed( JsonUtils.excludes(this.proService.findPageProvincialinformation(parameters, json.getInt("start"), json.getInt("pageSize")), new
					String[]{""}));
		}
		return result;
	}

	@POST
	@Path("addProvince")
	public String addProvince(String data ,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService, "provincename","provincecode","remark");
		if(result == null){
			ProvincialInformation pro = null;
			pro = new ProvincialInformation();
			JSONObject json = JsonUtils.converToJson(data);
			pro.setProvincecode(json.getString("provincecode").trim());
			pro.setProvincename(json.getString("provincename").trim());
			pro.setProvincestatus(Config.publicStatus[1]+"");
			pro.setRemark(json.getString("remark").trim());
			this.proService.addProvincialinformation(pro);
			return ResultMessage.getSucceed(null);
		}
		return result;
	}

	@POST
	@Path("updateProvince")
	public String updateProvince(String data ,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService, "provincename","provincecode","remark","provinceuuid");
		if(result == null){
			JSONObject json = JsonUtils.converToJson(data);
			ProvincialInformation pro = this.proService.findById(json.getString("provinceuuid"));
			if(pro != null){
				pro.setProvincecode(json.getString("provincecode").trim());
				pro.setProvincename(json.getString("provincename").trim());
				pro.setRemark(json.getString("remark").trim());
				this.proService.updateProvincialinformation(pro);
				return ResultMessage.getSucceed(null);
			}
			return ResultMessage.getFailt("");
		}
		return result;
	}


	@POST
	@Path("updateEnable")
	public String updateEnable(String data ,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService, "provincestatus","provinceuuid");
		if(result == null){
			JSONObject json = JsonUtils.converToJson(data);
			ProvincialInformation pro = this.proService.findById(json.getString("provinceuuid"));
			if(pro != null){
				pro.setProvincestatus(json.getString("provincestatus"));
				this.proService.updateProvincialinformation(pro);
				return ResultMessage.getSucceed(null);
			}
			return ResultMessage.getFailt("");
		}
		return result;
	}

	@POST
	@Path("findOnePro")
	public String findOnePro(String data ,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService, "provinceuuid");
		if(result == null){
			JSONObject json = JsonUtils.converToJson(data);
			return ResultMessage.getSucceed( JsonUtils.excludes(this.proService.findById(json.getString("provinceuuid").trim()), new String[]{
					"","","","","","","","","","","",""
			}));
		}
		return result;
	}

	@POST
	@Path("findAllPro")
	public String findAllPro(String data ,@Context HttpServletRequest request){
		JSONObject json = new JSONObject();
		json.put("test", "test");
		String result = this.validationParameter(json.toString(),"test");
		if(result == null){
			return ResultMessage.getSucceed( JsonUtils.jsonArrayToJson(this.proService.findAllPro(), new
					String[]{"createtime","provincestatus","remark"}));
		}
		return result;
	}

}
